package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.domain.query.MerchantQueryDO;
import com.recharge.domain.query.ProviderQueryDO;
import com.recharge.domain.vo.MerchantDO;
import com.recharge.domain.vo.ProviderDO;
import com.recharge.domain.vo.UserLoginDO;
import com.recharge.service.ProviderService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 供应商 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/provider")
public class ProviderController extends BaseController {
    
    @Autowired
    private ProviderService providerService;

    @RequiresPermissions("provider:add")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    @ApiOperation(value="增加供应商", notes="")
    public @ResponseBody
    ObjectResponse<ProviderDO> add(@RequestBody ProviderDO domain) {

        ObjectResponse<ProviderDO> response = new ObjectResponse<>();

        domain.setCreator(getUserName());

        ResultDO result = providerService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider:edit")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    @ApiOperation(value="编辑系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse<ProviderDO> edit(@RequestBody ProviderDO domain) {

        ObjectResponse<ProviderDO> response = new ObjectResponse<>();

        ResultDO result = providerService.edit(domain, getUserName());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider:get")
    @RequestMapping(value="/get.html", method = RequestMethod.POST)
    @ApiOperation(value="获取系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse<ProviderDO> get(@RequestBody ProviderDO domain) {

        ObjectResponse<ProviderDO> response = new ObjectResponse<>();

        ResultDO result = providerService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((ProviderDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("provider:getListByMerchantId")
    @RequestMapping(value="/getListByMerchantId.html", method = RequestMethod.POST)
    @ApiOperation(value="获取下游商家信息", notes="")
    public @ResponseBody
    ObjectResponse<List<ProviderDO>> getListByProviderId(@RequestBody ProviderQueryDO domain) {

        ObjectResponse<List< ProviderDO>> response = new ObjectResponse<>();

        ResultDO result = providerService.getListByMerchantId(domain.getMerchantId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((List<ProviderDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

    @RequiresPermissions("provider:delete")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    @ApiOperation(value="删除系统参数信息", notes="")
    public @ResponseBody
    ObjectResponse delete(@RequestBody ProviderDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = providerService.remove(domain.getId(), getUserName());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("provider:page")
    @RequestMapping(value="/page.html", method = RequestMethod.POST)
    @ApiOperation(value="分页查询系统参数", notes="")
    public @ResponseBody  ObjectResponse<PageDO> page(@RequestBody ProviderQueryDO query) {

        ObjectResponse<PageDO> response = new ObjectResponse<>();

        ResultDO result = providerService.find(query);

        if(result.isSuccess()) {
            response.setData((PageDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }

        return response;
    }

    @RequiresPermissions("provider:resetSecretKey")
    @RequestMapping(value="/resetSecretKey.html", method = RequestMethod.POST)
    @ApiOperation(value="重置卡密提供商密钥", notes="")
    public @ResponseBody
    ObjectResponse resetSecretKey(@RequestBody ProviderDO domain) {

        ObjectResponse response = new ObjectResponse();
        UserLoginDO userLoginDO = getUserLoginInfo();

        ResultDO result = providerService.resetSecretKey(domain.getId(), userLoginDO.getUsername());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
}
